class Solution:
    def numWays(self, s: str) -> int:
        ones = 0
        for string in s:
            if string == '1':
                ones += 1
        div, mod = divmod(ones, 3)
        if mod > 0:
            return 0
        if div == 0:
            zeros = len(s) - 2
            return ((zeros**2 + zeros) // 2) % (10**9 + 7)
        n = 0
        ones = 0
        for i in range(len(s)):
            if s[i] == '1':
                ones += 1
            if ones == div:
                n += 1
            elif ones > div:
                break
        m = 0
        ones = 0
        for i in range(len(s) - 1, -1, -1):
            if s[i] == '1':
                ones += 1
            if ones == div:
                m += 1
            elif ones > div:
                break
        return (m * n) % (10**9 + 7)
